<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\RoleListParam;
use App\Params\Backend\RoleUpdateMenuParam;
use App\Params\Backend\RoleUpdateParam;
use App\Params\Backend\RoleUpdateResourceParam;
use App\Services\Role\RoleService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 角色控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06-11
 */
class RoleController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 保存角色
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function update()
    {
        $param = RoleUpdateParam::create();
        $ret = RoleService::getInstance()->update($param);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::ROLE_UPDATE_FAIL);
    }

    /**
     * 角色列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = RoleListParam::create();
        $ret = RoleService::getInstance()->getList($param);
        return $this->success($ret);
    }

    /**
     * 停用/启用角色
     *
     * @param Request $request
     * @return mixed
     * @throws
     */
    public function enable(Request $request)
    {
        $id = $this->verifyInteger('role_id');
        $ret = RoleService::getInstance()->enable($id);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::ROLE_UPDATE_FAIL);
    }

    /**
     * 获取角色详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function get(Request $request)
    {
        $id = $this->verifyInteger('id');
        $ret = RoleService::getInstance()->get($id);
        return $this->success($ret);
    }

    /**
     * 获取角色绑定的资源
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function getResource()
    {
        $roleId = $this->verifyId('role_id');
        $ret = RoleService::getInstance()->getResourceBind($roleId);
        return $this->success($ret);
    }

    /**
     * 角色绑定资源
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function updateResource()
    {
        $param = RoleUpdateResourceParam::create();
        RoleService::getInstance()->updateResource($param);
        return $this->success();
    }

    /**
     * 获取角色绑定的菜单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function getMenu()
    {
        $roleId = $this->verifyInteger('role_id');
        $menus = RoleService::getInstance()->getMenuBind($roleId);
        return $this->success($menus);
    }

    /**
     * 保存角色菜单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function updateMenu()
    {
        $param = RoleUpdateMenuParam::create();
        RoleService::getInstance()->updateMenu($param);
        return $this->success();
    }
}
